<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/19
 * Time: 18:03
 */
namespace app\api\controller;
use think\Db;
class My extends Base {
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }

    /**
     * 修改个人信息
     * @return \think\response\Json
     */
    public function userInfoEdit(){
        extract(input());
        if(isset($username) && $username!=''){
            $data['user_username'] = $username;
        }
        if(isset($image) && $image!=''){
            $data['user_image'] = $image;
        }
        if(isset($sex) && $sex!=''){
            $data['user_sex'] = $sex;
        }
        if(isset($desc) && $desc!=''){
            $data['user_desc'] = $desc;
        }
        $data['user_updatetime'] = date('Y-m-d H:i:s');
        $where['user_id'] = $user_id;
        $res = Db::table('up_u_user')->where($where)->update($data);
        if($res){
            return json(array('status'=>200,'msg'=>"修改成功"));
        }else{
            return json(array('status'=>0,'msg'=>"修改失败"));
        }

    }
    /**
     * 充值
     * @return string|void
     */
    public function userRecharge(){
        extract(input());
        $today = date('Y-m-d H:i:s');
        $number = time() . rand(1000000,9999999);
        $order = [
            'order_user_id' => $user_id,
            'order_number' => $number,
            'order_user_pay' => $pay,
            'order_coin' => $pay*10,
            'order_remark' => $order_remark,
            'order_type' => $pay_type,
            'order_time' => $today
        ];
        if($pay_type == 1){
            $payorder = array();
            $payorder['body'] = '充值'; // 描述
            $payorder['total_fee'] = $order['order_user_pay']*100; // 订单金额
            $payorder['out_trade_no'] = $number; // 订单号
            $payorder['product_id'] = ''; // 产品ID
            $payorder['trade_type'] = 'APP'; // 类型
            $mww_weixinpay =  new \app\api\controller\Weixin();
            $respont = $mww_weixinpay->WeChatPayReady($payorder);
            return json(['status'=>200,'msg'=>$respont]);
        }elseif($pay_type == 2){
            $mww_alipay =  new \app\api\controller\Ali();
            $respont = $mww_alipay->getParam($number, $order['order_user_pay'], '充值', '充值');
            return json($respont);
        }

    }
    /**
     * 交易记录
     * 说明：
     * 1、当revenue_type为1时代表充值，revenue_money代表得到的怼币，revenue_pay代表实付的金额
     * 2、当revenue_type为2时代表打赏，
     *      revenue_user_id是我时，代表是我打赏别人;
     *      revenue_other_id是我时，代表别人打赏的我；
     *      revenue_money为打赏的怼币;who代表我打赏的谁或者谁打赏的我。
     *
     * @return \think\response\Json
     */
    public function myRecode(){
        extract(input());
        $list = Db::table('up_revenue')
            ->where(function ($query) use ($user_id) {
                $query->where('revenue_type', 1)->where('revenue_user_id', $user_id);
            })
            ->whereOr(function ($query) use ($user_id) {
                $query->where('revenue_type', '<>',1)->where('revenue_user_id|revenue_other_id', $user_id);
            })
            ->page($page,20)
            ->order('revenue_time desc')
            ->select();
        if($list){
            foreach ($list as $k => $v){
                $list[$k]['who'] = '';
                if($v['revenue_type'] == 2){
                    if($v['revenue_user_id'] == $user_id){
                        $need_uid = $v['revenue_other_id'];
                    }else{
                        $need_uid = $v['revenue_user_id'];
                    }
                    $who = Db::table('up_u_user')->where('user_id',$need_uid)->value('user_username');
                    $list[$k]['who'] = $who;
                }
            }
        }
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * 常见问题
     * @return \think\response\Json
     */
    public function questions(){
        extract(input());
        $where['q_status'] = 1;
        $list = Db::table('up_question')
            ->where($where)
            ->field('q_content',true)
            ->page($page,20)
            ->order('q_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * 常见问题详情
     * @return \think\response\Json
     */
    public function questionInfo(){
        extract(input());
        $where['q_id'] = $q_id;
        $list = Db::table('up_question')
            ->where($where)
            ->find();
        return json(['status'=>200,'msg'=>$list]);

    }
    /**
     * 意见反馈类型
     * @return \think\response\Json
     */
    public function feedbackType(){
        extract(input());
        $data = [
            'feedback_type_status' => 1
        ];
        $res = Db::table('up_feedback_type')->where($data)->select();
        return json(['status'=>200,'msg'=>$res]);

    }
    /**
     * 提交反馈
     * @return \think\response\Json
     */
    public function feedback(){
            extract(input());
        $data = [
            'f_user_id' => $user_id,
            'feedback_type_id' => $type_id,
            'feedback_content' => $content,
            'feedback_time' => date('Y-m-d H:i:s'),
        ];
        $res = Db::table('up_feedback')->insert($data);
        if($res){
            return json(array('status'=>200,'msg'=>"成功"));
        }else{
            return json(array('status'=>0,'msg'=>"失败"));
        }

    }
    /**
     * 邮箱和电话
     * @return \think\response\Json
     */
    public function phoneEmail(){
            extract(input());
        $phone = Db::table('up_about')->where('id',6)->value('content');
        $email = Db::table('up_about')->where('id',7)->value('content');
        $data = [
            'phone' => $phone,
            'email' => $email
        ];
        return json(array('status'=>200,'msg'=>$data));

    }
}